/**
 * JAVACC DEMO 1.0
 * @copy right iussoft company All rights reserved.
 * @Package com.iussoft.portal.send.method
 */
package com.apache.portal.common.template.method;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.apache.tools.DateUtils;
import com.apache.tools.StrUtil;
import com.apache.uct.common.ToolsUtil;

import freemarker.template.TemplateMethodModel;
import freemarker.template.TemplateModelException;

/**
 * description:  格式化Long类型时间
 */
public class FormatDateMethod implements TemplateMethodModel {

	/**
	 * TODO 简单描述该方法的实现功能（可选）.  
	 * @see freemarker.template.TemplateMethodModel#exec(java.util.List)  
	 */
	public Object exec(List arguments) throws TemplateModelException {
		if (!ToolsUtil.isEmpty(arguments)) {
			Long time = Long.valueOf(StrUtil.doNull(String.valueOf(arguments.get(0)), "0"));
			String simpleFormat = "";
			if (arguments.size() > 1)
				simpleFormat = String.valueOf(arguments.get(1));
			return long2TimezoneDefault(time, simpleFormat);
		}
		return "";
	}

	private String long2TimezoneDefault(Long timer, String simpleFormat) {
		if (timer == null || "0".equals(timer))
			return "";
		if (ToolsUtil.isNull(simpleFormat))
			simpleFormat = "yyyy-MM-dd HH:mm:ss";

		Date dt = DateUtils.fromLong(timer);
		if (dt == null) {
			return "";
		}
		SimpleDateFormat sdf = new SimpleDateFormat(simpleFormat);
		sdf.format(dt);
		return sdf.format(dt);
	}

	public static void main(String[] args) {
		long d = 1463717067280l;
		FormatDateMethod fd = new FormatDateMethod();
		System.out.println(fd.long2TimezoneDefault(d, "yyyy-MM-dd HH:mm:ss"));
	}

}
